[assembly: WebActivator.PreApplicationStartMethod(typeof(Example.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Example.Web.App_Start.NinjectWebCommon), "Stop")]

namespace Example.Web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using System.Collections.Generic;
    using System.Security.Principal;
    using Example.Persistence;
    using Applay;
    using Example.Application.Account;
    using Ninject.Activation.Strategies;
    using Ninject.Activation;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
        
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            
            var serviceWrapper = new Wrapper();
            serviceWrapper.EnableAuthorization(() => HttpContext.Current.User);
            kernel.Bind<Wrapper>().ToConstant(serviceWrapper);

            WrappedActionvationStrategy.Wrapper = serviceWrapper;
            kernel.Components.Add<IActivationStrategy, WrappedActionvationStrategy>();

            RegisterServices(kernel);

            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IEntities>().To<Entities>();

            kernel.Bind<AccountService>().ToSelf();
        }        
    }

    public class WrappedActionvationStrategy : ActivationStrategy
    {
        public static Wrapper Wrapper;

        public override void Activate(IContext context, InstanceReference reference)
        {
            if (typeof(Service).IsAssignableFrom(reference.Instance.GetType()))
            {
                reference.Instance = Wrapper.Wrap((Service)reference.Instance);
            }

            base.Activate(context, reference);
        }
    }
}
